package com.example.music_player_app.utils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

public class ApiServer {

    String APP_BASE_URL = "http://47.116.100.240:3000"; // 服务器地址

    OkHttpClient client = new OkHttpClient.Builder()
            .cookieJar(new CookieJar() {
                @NotNull
                @Override
                public List<Cookie> loadForRequest(@NotNull HttpUrl httpUrl) {
                    List<Cookie> cookies = CookieServer.getCookieList();
                    return cookies != null ? cookies : new ArrayList<>();
                }

                @Override
                public void saveFromResponse(@NotNull HttpUrl httpUrl, @NotNull List<Cookie> list) {
                    if (httpUrl.toString().equals("http://47.116.100.240:3000/login/cellphone")) {
                        String res = "";
                        for(Cookie item : list){
                            res += item;
                        }
                        CookieServer.setCookie(res);
                        CookieServer.setCookieList(list);
                        System.out.println(CookieServer.getCookie());
                    }
                }
            }).build();
    private final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(APP_BASE_URL)
            .client(client)
            .build();

    public Retrofit getRetrofit() {
        return retrofit;
    }

}
